package com.bootx.service.predict;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 方法五：历史相似轮次回放法（KNN 最近邻）
 * 思路：找到历史上红包总金额最接近的轮，直接用其结构预测下一轮。
 */
public class SimilarityPredictor {

    public List<Prediction> predict(List<RedPackageRound> rounds) {
        RedPackageRound last = rounds.get(rounds.size() - 1);
        double targetTotal = last.items.stream().mapToDouble(i -> i.amount).sum();

        RedPackageRound closest = rounds.stream()
                .min(Comparator.comparingDouble(r -> Math.abs(r.items.stream().mapToDouble(i -> i.amount).sum() - targetTotal)))
                .orElse(last);

        return closest.items.stream()
                .map(i -> new Prediction(i.order, i.amount, i.openTime))
                .collect(Collectors.toList());
    }
}
